package springboot.houbank.com.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by zhaoqiguo on 2016/9/12.
 */
public class PropertiesUtils {

    /**
     * (注意：加载的是src下的文件,如果在某个包下．请把包名加上)
     * @return
     */
    public static Properties getProperties(String sourcesFileName){
        Properties properties = new Properties();
        String savePath = Thread.currentThread().getContextClassLoader().getResource(sourcesFileName).getPath();
        try{
            InputStream inputStream = new BufferedInputStream(new FileInputStream(savePath));
            properties.load(inputStream);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  properties;
    }

    /**
     * 获取属性文件的数据 根据key获取值
     * @param key
     * @return
     */
    public static String findPorpertyByKey(String key,String sourcesFileName){

        try {
            Properties pro = getProperties(sourcesFileName);
            return pro.getProperty(key);
        }catch (Exception e){
            return "";
        }
    }
}
